package modes

import (
	"fmt"
)

/*
 * 描述: 题库选项表
 *
 *
 *****************************/

type TopicTextRe struct {
	Id    int64  `json:"id" xorm:"id"`         //内容ID
	TId   int64  `json:"t_id" xorm:"tid"`      //题ID
	TQues string `json:"t_ques" xorm:"ques"`   //题选项
	Selec string `json:"t_selec" xorm:"selec"` //答案选项
	Qid   string `json:"qid" xorm:"qid"`       //分片id
	NId   int64  `json:"n_id" xorm:"n_id"`     //套题ID
	// 前端需要
	IsSele int `json:"is_sele" xorm:"-"` //此选项是否选中 默认0 选中：1
}

func (this *TopicTextRe) TableName() string {
	return "topictextre"
}

func (this *TopicTextRe) Save() (int64, error) {
	return Db(0).Insert(this)
}

func (this *TopicTextRe) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *TopicTextRe) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *TopicTextRe) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TopicTextRe) Count(where string) (int64, error) {
	var val TopicTextRe
	return Db(0).Where(where).Count(&val)
}
func (this *TopicTextRe) GetList(nidList []int64) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	err := Db(0).Asc("selec").In("n_id", nidList).Find(&list)
	return list, err
}
func (this *TopicTextRe) GetListByNid(nidList int64) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	err := Db(0).Asc("selec").Where("n_id=?", nidList).Find(&list)
	return list, err
}
func (this *TopicTextRe) GetListByNidList(nidList []int64) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	err := Db(0).Asc("selec").In("n_id", nidList).Find(&list)
	return list, err
}

func (this *TopicTextRe) GetListByQidList(tidList []int64) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	err := Db(0).Asc("tid").In("qid", tidList).Find(&list)
	return list, err
}

func (this *TopicTextRe) GetListByidList(tidList []int64) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	err := Db(0).Asc("selec").In("qid", tidList).Find(&list)
	return list, err
}
func (this *TopicTextRe) where(fage, count, page int, where, field string) ([]TopicTextRe, error) {
	list := make([]TopicTextRe, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
